from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from story.models import Story
from interactive.models import Report

class AdminLog(models.Model):
    ACTION_TYPES = [
        ('delete_story', '删除故事'),
        ('ban_user', '封禁用户'),
        ('handle_report', '处理举报'),
        ('edit_story', '编辑故事'),
        ('admin_login', '管理员登录'),
        ('admin_logout', '管理员注销'),
        ('update_settings', '更新设置'),
        ('approve_comment', '批准评论'),
        ('reject_comment', '拒绝评论'),
        ('delete_comment', '删除评论'),
        ('approve_comments', '批量批准评论'),
        ('reject_comments', '批量拒绝评论'),
        ('delete_comments', '批量删除评论'),
        ('dismiss_reports', '批量驳回举报'),
        ('delete_stories', '批量删除故事'),
        ('other', '其他操作'),
    ]
    
    admin = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='管理员')
    action_type = models.CharField(max_length=20, choices=ACTION_TYPES, verbose_name='操作类型')
    target_story = models.ForeignKey(Story, on_delete=models.CASCADE, null=True, blank=True, verbose_name='目标故事')
    target_user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name='admin_actions', verbose_name='目标用户')
    report = models.ForeignKey(Report, on_delete=models.CASCADE, null=True, blank=True, verbose_name='相关举报')
    description = models.TextField(verbose_name='操作描述')
    created_at = models.DateTimeField(default=timezone.now, verbose_name='操作时间')
    
    class Meta:
        verbose_name = '管理员日志'
        verbose_name_plural = '管理员日志'
        ordering = ['-created_at']
    
    def __str__(self):
        return f'{self.admin.username}的{self.get_action_type_display()}操作'

class SystemConfig(models.Model):
    key = models.CharField(max_length=50, unique=True, verbose_name='配置键')
    value = models.JSONField(verbose_name='配置值')
    description = models.TextField(verbose_name='配置描述')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        verbose_name = '系统配置'
        verbose_name_plural = '系统配置'
    
    def __str__(self):
        return self.key

class Statistics(models.Model):
    date = models.DateField(unique=True, verbose_name='统计日期')
    daily_active_users = models.IntegerField(default=0, verbose_name='日活跃用户数')
    new_users = models.IntegerField(default=0, verbose_name='新增用户数')
    new_stories = models.IntegerField(default=0, verbose_name='新增故事数')
    total_views = models.IntegerField(default=0, verbose_name='总浏览量')
    total_likes = models.IntegerField(default=0, verbose_name='总点赞数')
    total_comments = models.IntegerField(default=0, verbose_name='总评论数')
    
    class Meta:
        verbose_name = '统计数据'
        verbose_name_plural = '统计数据'
        ordering = ['-date']
    
    def __str__(self):
        return f'{self.date}的统计数据'
